package com.leoandroid.tool.toolsbox.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.leoandroid.tool.toolsbox.bean.DiscernInfoBean;
import com.leoandroid.tool.toolsbox.largeread.R;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;

public class DiscernInfoAdapter extends RecyclerView.Adapter<DiscernInfoAdapter.ViewHolder> {
    private final ArrayList<DiscernInfoBean> dataList;
    private final Context context;

    public static class ViewHolder extends RecyclerView.ViewHolder {

        TextView itemScore;
        TextView itemName;
        TextView itemDesc;

        public ViewHolder(View view) {
            super(view);
            itemScore = view.findViewById(R.id.item_score);
            itemName = view.findViewById(R.id.item_name);
            itemDesc = view.findViewById(R.id.item_desc);
        }
    }

    public DiscernInfoAdapter(Context context, ArrayList<DiscernInfoBean> arrayList) {
        this.context = context;
        this.dataList = arrayList;
    }

    public final Context getContext() {
        return context;
    }

    public int getItemCount() {
        if (dataList == null || dataList.size() <= 0) {
            return 0;
        }
        return dataList.size();
    }

    public void onBindViewHolder(@NotNull DiscernInfoAdapter.ViewHolder viewHolder, int i) {
        DiscernInfoBean discernInfoBean = getDataList().get(i);
        if (!TextUtils.isEmpty(discernInfoBean.getScore())) {
            viewHolder.itemScore.setVisibility(View.VISIBLE);
            viewHolder.itemScore.setText("相似度：" + discernInfoBean.getScore());
        } else {
            viewHolder.itemScore.setVisibility(View.GONE);
        }
        viewHolder.itemName.setText(discernInfoBean.getName());
        if (discernInfoBean.getBaike_info() == null || TextUtils.isEmpty(discernInfoBean.getBaike_info().getDescription())) {
            viewHolder.itemDesc.setVisibility(View.GONE);
            return;
        }
        if (TextUtils.isEmpty(discernInfoBean.getBaike_info().getDescription())) {
            viewHolder.itemDesc.setVisibility(View.GONE);
        } else {
            viewHolder.itemDesc.setVisibility(View.VISIBLE);
            viewHolder.itemDesc.setText(discernInfoBean.getBaike_info().getDescription());
        }
    }

    public ArrayList<DiscernInfoBean> getDataList() {
        return dataList;
    }

    public ViewHolder onCreateViewHolder(@NotNull ViewGroup viewGroup, int i) {
        View root = LayoutInflater.from(context).inflate(R.layout.discern_item_layout, viewGroup, false);
        return new ViewHolder(root);
    }
}
